import { moduleChange, commonDict ,RLogFace, stateNotification} from "/@/api/index"
import { Toast } from '/@/utils/Message'
import { sm2 } from "sm-crypto"
/**
     * @description 用户行为
     * @param{String}uuid 后台接口返回的uuid
     * @param{String}entryOrExit 1进入模块 2 退出模块
     * @param{String}modelName 模块名字
     * @param callback 回调函数
     **/
export function recordUserBehavior(uuid:any, entryOrExit:any, modelName:any, callback:any) {
    const currentTime = getLocalTime()
    const data = {
        id: uuid,
        userId:"",
        type:"",
        modelEntryTime:'',
        modelExitTime:'',
        modelName:modelName,
        phoneSystem:""
    }
    if(entryOrExit == '1') {
        data.modelEntryTime = currentTime
        data.modelExitTime = ''
    }else{
        data.modelEntryTime = ''
        data.modelExitTime = currentTime
    }
    moduleChange(data).then((res:any) => {
        if(res.code == 200) {
            const uuid = res.data.id
            if(uuid) {
                callback(uuid)
            }
        }
       
    })

}
function getLocalTime() {
    const d = new Date() // 根据时间戳生成的时间对象
    const date = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`
    return date
}

// 是否显示导航栏和状态栏
export const isShowStatusAndNavigator = ():number => {
    if(localStorage.getItem('statusBar') == 'true' && localStorage.getItem('navigator') == 'true') {
        return 2
    }else if(localStorage.getItem('statusBar') == 'true' || localStorage.getItem('navigator') == 'true') {
        return 1
    }else {
        return 0
    }
}

// 获取身份证类型
export const getIdType = (idType:any):Promise<string> => {
    return new Promise((resolve) => {
        commonDict("certi_type").then((res:any) => {
            if(res.code == 200) {   
                const codeName = res.data.find((item:any) => item.codeCode == idType)?.codeName
                resolve(codeName)
            }else {
                Toast("证件类型获取失败")
            }
        })
    })
}

// sm2 加密
export const sm2Encryption = async(key:any, data:any) => {  
    const publicKey = key
    const cipherMode = 1
    const result = sm2.doEncrypt(data, publicKey, cipherMode)
    return `04${result}`

}

export function recordLogFace(taskNo:any) { // 人脸之后记录日志
    RLogFace(taskNo).then((res:any) => {
        if(res.code == 200) {
            // console.log(res);
        } 
    })

}

export function recordStateNotification(taskNo:any) {
    return new Promise((relove, reject) => {
        stateNotification(taskNo).then((res:any) => {
            if(res.code == 200) {
                relove(res)
            }else {
                reject(res.msg)
            }
        })
    })
}